rm(list =ls())
options(scipen=999)
gc()
packages <-c("tidyverse","estimatr","plm","stargazer",
             "fastDummies","ICCbin","ihs","readstata13","xtable")

new.packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

lapply(packages, require, character.only = TRUE)
rm(packages, new.packages)

setwd("PUT YOUR DIRECTORY HERE")

## load data sets
main_confounders <- read.csv("./Datasets/main_confounders.csv")
distance_commanders <- read.csv("./Datasets/matching_distance_commanders.csv")

## merge both data sets
data <- merge(main_confounders, distance_commanders, by = "locality")
rm(main_confounders, distance_commanders)

# generate surveilance and strike variables
data_1 <- data%>%
  dplyr::rowwise()%>%
  dplyr::mutate(commander = sum(c(commanders1975, commanders1976, commanders1977, 
                                  commanders1978, commanders1979), na.rm = TRUE))%>%
  dplyr::ungroup()%>%
  dplyr::mutate(commander_binary = ifelse(commander <= 0,0,1))%>%
  dplyr::mutate(commander_binary = scale(commander_binary))%>%
  dplyr::mutate(strikes = scale(strikes))
  


data_2 <- data%>%
  dplyr::rowwise()%>%
  dplyr::mutate(commander = sum(c(commanders1980, commanders1981, commanders1982,
                                  commanders1983, commanders1984, commanders1984,
                                  commanders1985, commanders1986), na.rm = TRUE))%>%
  dplyr::ungroup()%>%
  dplyr::mutate(commander_binary = ifelse(commander <= 0,0,1))%>%
  dplyr::mutate(commander_binary = scale(commander_binary))%>%
  dplyr::mutate(strikes = scale(strikes))
  
#generate sabotage variable 
data_1 <- data_1%>%
  dplyr::mutate(across(subbotnik_inkind_1975:subbotnik_inkind_1979, scale))%>%
  dplyr::rowwise()%>%
  dplyr::mutate(compliance = mean(c(subbotnik_inkind_1975,subbotnik_inkind_1976,
                                    subbotnik_inkind_1977,subbotnik_inkind_1978,
                                    subbotnik_inkind_1979), na.rm = TRUE))%>%
  dplyr::ungroup()%>%
  dplyr::mutate(sabotage = scale(compliance * -1))

data_2 <- data_2%>%
  dplyr::mutate(across(subbotnik_inkind_1975:subbotnik_inkind_1979, scale))%>%
  dplyr::rowwise()%>%
  dplyr::mutate(compliance = mean(c(subbotnik_inkind_1975,subbotnik_inkind_1976,
                                    subbotnik_inkind_1977,subbotnik_inkind_1978,
                                    subbotnik_inkind_1979), na.rm = TRUE))%>%
  dplyr::ungroup()%>%
  dplyr::mutate(sabotage = scale(compliance * -1))

#generate interaction variable 
data_1 <- data_1%>%
  dplyr::mutate(commander_distance = scale(commander_binary) * scale(distance_commanders))

data_2 <- data_2%>%
  dplyr::mutate(commander_distance = scale(commander_binary) * scale(distance_commanders))


#fit models 
lm_strikes <- plm(strikes ~ commander_binary, index = c("distance_commanders"), 
                  model = c("within"), data = data_1)
lm_strikes_int <- plm(strikes ~ commander_binary + commander_distance, index = c("distance_commanders"), 
                      model = c("within"), data = data_1)


lm_sabotage <- plm(sabotage ~ commander_binary, index = c("distance_commanders"), 
                     model = c("within"), data = data_2)

lm_sabotage_int <- plm(sabotage ~ commander_binary + commander_distance, index = c("distance_commanders"), 
                         model = c("within"), data = data_2)


## save as tex
stargazer(lm_strikes, lm_sabotage, lm_strikes_int, lm_sabotage_int,
          title = "Table A7: Effect of surveillance on resistance (geographic matching)",
          dep.var.labels = c("Protests", "Sabotage","Protests","Sabotage"),
          style = "qje",
          covariate.labels = c("Surveillance","Surveillance x Distance"),
          star.char = c("*", "**", "***"),
          star.cutoffs = c(0.1, 0.05, 0.01),
          omit = c("Constant"),
          omit.stat = c("rsq", "f", "ser"), 
          omit.table.layout = "n",
          out = "PUT YOUR FILEPATH HERE")



